﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DotNet.Core.Entity;
using DotNet.Core.Service;
using DotNet.Helper;
using DotNet.Utility;
using DotNet.Extensions;
using DotNet.JCore.Service;
using DotNet.JCore.Entity;
using DotNet.Auth.Utility;

namespace DotNet.Web.Controllers
{
    public class CheckPointsController : BaseController
    {
        private CheckPointsService service = new CheckPointsService();
        // GET: CheckPoints
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Grid(string name, string enabled)
        {
            var list = service.GetPageList(PageInfo(), name, enabled.ToBoolOrNull());
            return View(list);
        }
        public ActionResult Create()
        {
            MarkCreate();
            return EditCore(new CheckPoints
            {
                Id = StringHelper.Guid()
            });
        }
        public ActionResult Edit(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(CheckPoints entity)
        {
            return View("Edit", entity);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save(CheckPoints entity)
        {
            AuthHelper.SetJCUserInfo(entity, IsCreate);
            var result = service.Save(entity, IsCreate);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            service.Delete(id.SplitToArray());
            return Json(BoolMessage.True);
        }

        public ActionResult Details(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }

        public ActionResult Export(string name, string enabled)
        {
            return Export(service.GetList(name, enabled.ToBoolOrNull()));
        }


        private ActionResult NotFound(string id)
        {
            return NotFound("获取 稽查点 错误", $"无法找到 Id = {id} 的稽查点信息");
        }
    }
}